package com.yhm.myfootbollshow.demos.service.serviceimpl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhm.myfootbollshow.demos.entity.dto.PageBase;
import com.yhm.myfootbollshow.demos.entity.po.Product;
import com.yhm.myfootbollshow.demos.entity.po.ProductReview;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceCarousel;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceDetail;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceDetailAndComment;
import com.yhm.myfootbollshow.demos.entity.vo.ProduceFoundation;
import com.yhm.myfootbollshow.demos.mapper.*;
import com.yhm.myfootbollshow.demos.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yhm
 * @time: 2025/4/4 17:52
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private BrandMapper brandMapper;

    @Resource
    private ProductReviewMapper productReviewMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public List<ProduceCarousel> listCarousel() {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Product::getProductId, Product::getMainImage);
        wrapper.eq(Product::getCarousel, 1)
                .eq(Product::getStatus, 1);
        List<Product> list = this.list(wrapper);
        return list.stream().map(
                product -> new ProduceCarousel()
                        .setId(product.getProductId())
                        .setImage(product.getMainImage())
        ).collect(Collectors.toList());
    }

    @Override
    public List<ProduceFoundation> listFoundation(String categoryId, String name, ArrayList<Integer> priceRange) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Product::getProductId,
                        Product::getProductName,
                        Product::getCategoryId,
                        Product::getMainImage,
                        Product::getBrandId,
                        Product::getPrice,
                        Product::getOriginalPrice,
                        Product::getSoldNum,
                        Product::getStock,
                        Product::getSpecs)
                .eq(Product::getStatus, 1);
        if (categoryId != null && !categoryId.isEmpty() && !categoryId.equals("0")) {
            wrapper.eq(Product::getCategoryId, categoryId);
        }
        if (name != null && !name.isEmpty()) {
            wrapper.like(Product::getProductName, name);
        }
        if (priceRange.size() == 2 && priceRange.get(0) != 0 && priceRange.get(1) != 0){
            wrapper.between(Product::getPrice, priceRange.get(0), priceRange.get(1));
        }
        List<Product> list = this.list(wrapper);
        return list.stream().map(
                product -> new ProduceFoundation()
                        .setId(product.getProductId())
                        .setName(product.getProductName())
                        .setBrand(brandMapper.getBrandNameById(product.getBrandId()))
                        .setPrice(product.getPrice())
                        .setOriginalPrice(product.getOriginalPrice())
                        .setImage(product.getMainImage())
                        .setRating(product.getSoldNum().toString())
                        .setSales(product.getSoldNum().toString())
                        .setStock(product.getStock().toString())
                        .setCategoryId(product.getCategoryId().toString())
                        .setSizes(JSONUtil.parseObj(product.getSpecs()).get("尺码").toString())
                        .setColors(JSONUtil.parseObj(product.getSpecs()).get("颜色").toString())
                        .setGenders(JSONUtil.parseObj(product.getSpecs()).get("性别").toString())
        ).collect(Collectors.toList());
    }

    @Override
    public ProduceDetailAndComment getProductDetailAndComment(Long productId) {
        // 判断 id是否为空
        if (productId == null) {
            return null;
        }

        // 查询商品详情
        Product product = baseMapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getProductId, productId));
        if (product == null) {
            return null;
        }

        // 查询商品评论
        List<ProductReview> productReviews = productReviewMapper.selectList(new LambdaQueryWrapper<ProductReview>()
                .eq(ProductReview::getProductId, productId));

        // 组装 ProduceDetailAndComment 并返回
        ProduceDetailAndComment detailAndComment = new ProduceDetailAndComment();
        detailAndComment.setProduceDetail(new ProduceDetail()
                .setProductId(product.getProductId())
                .setCategoryId(product.getCategoryId())
                .setBrandId(product.getBrandId())
                .setBrand(brandMapper.getBrandNameById(product.getBrandId()))
                .setProductName(product.getProductName())
                .setPrice(product.getPrice().toString())
                .setOriginalPrice(product.getOriginalPrice().toString())
                .setStock(product.getStock())
                .setSoldNum(product.getSoldNum())
                .setMainImage(product.getMainImage())
                .setDetail(product.getDetail())
                .setSubImages(product.getSubImages())
                .setSpecs(product.getSpecs())
        );
        detailAndComment.setProductReviews(productReviews);
        return detailAndComment;
    }

    @Override
    public IPage<Product> list(PageBase page) {
        // 判断page是否为空
        if (page == null) {
            return null;
        }
        Page<Product> pages = new Page<>();
        pages.setCurrent(page.getPageNum());
        pages.setSize(page.getPageSize());
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(page.getKeyword() != null, Product::getProductName, page.getKeyword())
                .eq(page.getStatus() != null && !page.getStatus().isEmpty(), Product::getStatus, page.getStatus());
        List<Product> products = baseMapper.selectList(pages,wrapper);
        products.forEach(item ->{
            item.setBrandName(brandMapper.selectById(item.getBrandId()).getBrandName());
            item.setCategoryName(categoryMapper.selectById(item.getCategoryId()).getCategoryName());
            item.setMerchantName(merchantMapper.selectById(item.getMerchantId()).getMerchantName());
        });
        return pages.setRecords(products);
    }
}
